package com.rr.mymoney.security;

import java.util.Collection;
import java.util.HashSet;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.rr.mymoney.model.User;

public class MyUserDetails implements UserDetails {

    private static final long serialVersionUID = 1L;

    private User user;
    private Collection<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();

    public MyUserDetails(User user, String[] rights) {
	super();
	this.user = user;
	for (String smartRight : rights) {
	    grantedAuthorities.add(new SimpleGrantedAuthority(smartRight));
	}
    }

    public Collection<GrantedAuthority> getAuthorities() {
	return grantedAuthorities;
    }

    public String getPassword() {
	return user.getPassword();
    }

    public String getUsername() {
	return user.getUsername();
    }

    public boolean isAccountNonExpired() {
	return true;
    }

    public boolean isAccountNonLocked() {
	return true;
    }

    public boolean isCredentialsNonExpired() {
	return true;
    }

    public boolean isEnabled() {
	return true;
    }

    public User getUser() {
	return user;
    }

    @Override
    public String toString() {
	return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
    }
}
